<?php
class ModelCataloghotrotructuyen extends Model {
	public function addhotrotructuyen($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "hotrotructuyen SET sort_order = '" . (int)$this->request->post['sort_order'] . "', danhmuchotro_id = '" . (int)$data['danhmuchotro_id'] . "'");

		$hotrotructuyen_id = $this->db->getLastId(); 
			
		foreach ($data['hotrotructuyen_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "hotrotructuyen_description SET hotrotructuyen_id = '" . (int)$hotrotructuyen_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', nickyahoo = '" . $this->db->escape($value['nickyahoo']) . "', nickskype = '" . $this->db->escape($value['nickskype']) . "',sodienthoai= '" . $this->db->escape($value['sodienthoai']) . "'");
		}
		
		$this->cache->delete('hotrotructuyen');
	}
	
	public function edithotrotructuyen($hotrotructuyen_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "hotrotructuyen SET sort_order = '" . (int)$data['sort_order'] . "', danhmuchotro_id = '" . (int)$data['danhmuchotro_id'] . "' WHERE hotrotructuyen_id = '" . (int)$hotrotructuyen_id . "'");

		$this->db->query("DELETE FROM " . DB_PREFIX . "hotrotructuyen_description WHERE hotrotructuyen_id = '" . (int)$hotrotructuyen_id . "'");
					
		foreach ($data['hotrotructuyen_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "hotrotructuyen_description SET hotrotructuyen_id = '" . (int)$hotrotructuyen_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', nickyahoo = '" . $this->db->escape($value['nickyahoo']) . "', nickskype = '" . $this->db->escape($value['nickskype']) . "',sodienthoai= '" . $this->db->escape($value['sodienthoai']) . "'");
		}
		
		$this->cache->delete('hotrotructuyen');
	}
	
	public function deletehotrotructuyen($hotrotructuyen_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "hotrotructuyen WHERE hotrotructuyen_id = '" . (int)$hotrotructuyen_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "hotrotructuyen_description WHERE hotrotructuyen_id = '" . (int)$hotrotructuyen_id . "'");

		$this->cache->delete('hotrotructuyen');
	}	

	public function gethotrotructuyen($hotrotructuyen_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "hotrotructuyen WHERE hotrotructuyen_id = '" . (int)$hotrotructuyen_id . "'");
		
		return $query->row;
	}
		
	public function gethotrotructuyens($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "hotrotructuyen i LEFT JOIN " . DB_PREFIX . "hotrotructuyen_description id ON (i.hotrotructuyen_id = id.hotrotructuyen_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		
			$sort_data = array(
				'id.title',
				'i.sort_order'
			);		
		
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY id.title";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}		

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
			
			return $query->rows;
		} else {
			$hotrotructuyen_data = $this->cache->get('hotrotructuyen.' . $this->config->get('config_language_id'));
		
			if (!$hotrotructuyen_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "hotrotructuyen i LEFT JOIN " . DB_PREFIX . "hotrotructuyen_description id ON (i.hotrotructuyen_id = id.hotrotructuyen_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY id.title");
	
				$hotrotructuyen_data = $query->rows;
			
				$this->cache->set('hotrotructuyen.' . $this->config->get('config_language_id'), $hotrotructuyen_data);
			}	
	
			return $hotrotructuyen_data;			
		}
	}
	
	public function gethotrotructuyenDescriptions($hotrotructuyen_id) {
		$hotrotructuyen_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "hotrotructuyen_description WHERE hotrotructuyen_id = '" . (int)$hotrotructuyen_id . "'");

		foreach ($query->rows as $result) {
			$hotrotructuyen_description_data[$result['language_id']] = array(
				'title'       => $result['title'],
				'nickyahoo'       => $result['nickyahoo'],
				'nickskype'       => $result['nickskype'],
				'sodienthoai' => $result['sodienthoai']
			);
		}
		
		return $hotrotructuyen_description_data;
	}
	
	public function getTotalProductsBydanhmuchotroId($danhmuchotro_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "hotrotructuyen WHERE danhmuchotro_id = '" . (int)$danhmuchotro_id . "'");

		return $query->row['total'];
	}
	
	public function getTotalhotrotructuyens() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "hotrotructuyen");
		
		return $query->row['total'];
	}	
}
?>